package com.cat.dataStructure03;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimum-operations-to-make-array-equal-ii/
 * @create 2025/11/8 10:01
 * @since JDK17
 */

public class Solution21 {
    public long minOperations(int[] nums1, int[] nums2, int k) {
        int n = nums1.length;
        if (k == 0) {
            for (int i = 0; i < n; i++) {
                if (nums1[i] != nums2[i]) {
                    return -1;
                }
            }
            return 0;
        }

        long add = 0, sub = 0;
        for (int i = 0; i < n; i++) {
            int x = Math.abs(nums1[i] - nums2[i]);
            if (x % k != 0) {
                return -1;
            }
            if (nums1[i] > nums2[i]) {
                add += x / k;
            } else {
                sub += x / k;
            }
        }

        return sub == add ? sub : -1;
    }
}
